home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / regcode / registry.bas < prev    next >
Encoding:
BASIC Source File  |  1998-08-11  |  19.1 KB  |  455 lines

  1. Attribute VB_Name = "RegistryModule"
  2. Option Explicit
  3. ' --------------------------------------------------------
  4. ' Module    : RegistryModule(Registry.bas)
  5. ' Written by: Elito C. Lelina III
  6. '             ECLIPSE Development Software
  7. ' URL       : www.geocities.com/SiliconValley/Campus/3118/
  8. ' email     : eclipseds@hotmail.com
  9. ' --------------------------------------------------------
  10. '
  11. ' This module contains functions for reading and
  12. ' setting registry values of type REG_SZ and REG_DWORD
  13. ' in Windows 95 and Windows NT. Code can be modified
  14. ' to handle other Value type.
  15. '
  16. ' This program makes no guarantees and no support is provided,
  17. ' but comments/bug reports are welcome.
  18. '
  19. ' Warning:  Windows depends heavily on Registry Data file.
  20. ' Editing registry values can seriously impact Windows and
  21. ' your machine's operations. Create Registry Backup before
  22. ' editing. You should only edit values when you know what
  23. ' they should be.  If editing values as a test, make a note
  24. ' of the original value and restore it when you are done.
  25.  
  26. ' Project Registry Const
  27. Public Const regAutoStartPath = "Software\Microsoft\Windows\CurrentVersion\Run"
  28. Public Const regAppName = "ECLRegDemo"
  29. Public Const regAppKeyName = "Software\ECLIPSE Development Software\" & regAppName
  30.  
  31. ' --------------------------------------------------------
  32. ' FILETIME type is needed for RegEnumKey and RegQueryInfoKey
  33. ' --------------------------------------------------------
  34. Private Type FILETIME
  35.     lLowDateTime    As Long
  36.     lHighDateTime   As Long
  37. End Type
  38.  
  39. ' --------------------------------------------------------
  40. ' Registry Root Keys. Most Programs would use HKEY_CURRENT_USER
  41. ' and HKEY_LOCAL_MACHINE for storing settings. If you want to
  42. ' retain settings for individual user (Machine should be configured
  43. ' for multiple users), store settings in CURRENT_USER.
  44. ' HKEY_CLASSES_ROOT contains information about Application file
  45. ' types.
  46. ' --------------------------------------------------------
  47. Public Enum RegRootKeys
  48.     HKEY_CLASSES_ROOT = &H80000000
  49.     HKEY_CURRENT_USER = &H80000001
  50.     HKEY_LOCAL_MACHINE = &H80000002
  51.     HKEY_USERS = &H80000003
  52.     HKEY_PERFORMANCE_DATA = &H80000004
  53.     HKEY_CURRENT_CONFIG = &H80000005
  54.     HKEY_DYN_DATA = &H80000006
  55. End Enum
  56.  
  57. ' --------------------------------------------------------
  58. ' Registry Data types. This module supports only REG_SZ and
  59. ' REG_DWORD Data types. Uncomment other data type when modifying
  60. ' this module to support other types
  61. ' --------------------------------------------------------
  62. Public Enum RegDataTypes
  63.     REG_NONE = 0&                  ' No value type
  64.     REG_SZ = 1&                    ' Unicode null terminated string
  65. '    REG_EXPAND_SZ = 2&             ' Unicode null terminated string (with environment variable references)
  66. '    REG_BINARY = 3&                ' Free form binary
  67.     REG_DWORD = 4&                 ' 32-bit number
  68. '    REG_DWORD_LITTLE_ENDIAN = 4&   ' 32-bit number (same as REG_DWORD)
  69. '    REG_DWORD_BIG_ENDIAN = 5&      ' 32-bit number
  70. '    REG_LINK = 6&                  ' Symbolic Link (unicode)
  71. '    REG_MULTI_SZ = 7&              ' Multiple Unicode strings
  72. '    REG_RESOURCE_LIST = 8&         ' Resource list in the resource map
  73. '    REG_FULL_RESOURCE_DESCRIPTOR = 9&    ' Resource list in the hardware description
  74. '    REG_RESOURCE_REQUIREMENTS_LIST = 10&
  75. End Enum
  76.  
  77. ' --------------------------------------------------------
  78. ' Return codes from Registration functions.
  79. ' --------------------------------------------------------
  80. Private Const ERROR_SUCCESS = 0&
  81. Private Const ERROR_BADDB = 1009&
  82. Private Const ERROR_BADKEY = 1010&
  83. Private Const ERROR_CANTOPEN = 1011&
  84. Private Const ERROR_CANTREAD = 1012&
  85. Private Const ERROR_CANTWRITE = 1013&
  86. Private Const ERROR_OUTOFMEMORY = 14&
  87. Private Const ERROR_INVALID_PARAMETER = 87&
  88. Private Const ERROR_ACCESS_DENIED = 5&
  89. Private Const ERROR_NO_MORE_ITEMS = 259&
  90. Private Const ERROR_MORE_DATA = 234&
  91.  
  92. ' --------------------------------------------------------
  93. ' Read/Write permissions:
  94. ' --------------------------------------------------------
  95. Private Const REG_OPTION_NON_VOLATILE = 0
  96. Private Const KEY_QUERY_VALUE = &H1
  97. Private Const KEY_SET_VALUE = &H2
  98. Private Const KEY_CREATE_SUB_KEY = &H4
  99. Private Const KEY_ENUMERATE_SUB_KEYS = &H8
  100. Private Const KEY_NOTIFY = &H10
  101. Private Const KEY_CREATE_LINK = &H20
  102. Private Const SYNCHRONIZE = &H100000
  103. Private Const STANDARD_RIGHTS_ALL = &H1F0000
  104. Private Const READ_CONTROL = &H20000
  105. Private Const WRITE_DAC = &H40000
  106. Private Const WRITE_OWNER = &H80000
  107. Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
  108. Private Const STANDARD_RIGHTS_READ = READ_CONTROL
  109. Private Const STANDARD_RIGHTS_WRITE = READ_CONTROL
  110. Private Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
  111. Private Const KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
  112. Private Const KEY_WRITE = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
  113. Private Const KEY_EXECUTE = KEY_READ
  114.  
  115. Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
  116.    KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or _
  117.    KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
  118.  
  119. Type SECURITY_ATTRIBUTES
  120.     nLength As Long
  121.     lpSecurityDescriptor As Long
  122.     bInheritHandle As Boolean
  123. End Type
  124.  
  125. ' ---------------------------
  126. ' 32-bit registry functions
  127. ' ---------------------------
  128. Private Declare Function RegCreateKey Lib "advapi32" Alias "RegCreateKeyA" (ByVal hkey As Long, ByVal lpszSubKey As String, phkResult As Long) As Long
  129. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
  130. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hkey As Long, ByVal lpSubKey As String) As Long
  131. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hkey As Long, ByVal lpValueName As String) As Long
  132. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
  133. Private Declare Function RegEnumKeyEx Lib "advapi32" Alias "RegEnumKeyA" (ByVal hkey As Long, ByVal iSubKey As Long, ByVal lpszName As String, ByVal cchName As Long) As Long
  134. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hkey&, ByVal lpszSubKey$, dwOptions&, ByVal samDesired&, lpHKey&) As Long
  135. Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hkey As Long, ByVal lpszValueName As String, ByVal dwReserved As Long, lpdwType As Long, lpbData As Any, cbData As Long) As Long
  136. Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpszValueName As String, ByVal dwReserved As Long, ByVal fdwType As Long, lpbData As Any, ByVal cbData As Long) As Long
  137. Private Declare Function RegSetStringEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpszValueName As String, ByVal dwReserved As Long, ByVal fdwType As Long, lpbData As String, ByVal cbData As Long) As Long
  138. Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
  139.  
  140. Private Declare Function RegEnumValue& Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hkey&, ByVal dwIndex&, ByVal lpName$, lpcbName&, ByVal lpReserved&, lpdwType&, lpValue As Any, lpcbValue&)
  141. Private Declare Function RegQueryInfoKey& Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hkey&, ByVal lpClass$, lpcbClass&, ByVal lpReserved&, lpcSubKeys&, lpcbMaxSubKeyLen&, lpcbMaxClassLen&, lpcValues&, lpcbMaxValueNameLen&, lpcbMaxValueLen&, lpcbSecurityDescriptor&, lpftLastWriteTime As FILETIME)
  142.  
  143. ' Registry content value holder (use in obtaining value)
  144. Public regValue As Variant
  145.  
  146. ' --------------------------------------------------------
  147. ' Purpose   : Check if a certain key exist in the registry
  148. '             hkey = Registry Root key
  149. '             SubKeyPath = String containing the keypath to query
  150. ' Return    : True = Function Successful
  151. '             False = Function Failed
  152. ' Example   : result = regKeyExist(HKEY_LOCAL_MACHINE,_
  153. '                      "Software\ECLIPSE Development Software\")
  154. ' --------------------------------------------------------
  155. Public Function regKeyExist(hkey As RegRootKeys, SubKeyPath As String) As Boolean
  156. Dim lresult As Long
  157. Dim phkResult As Long
  158.     ' Opens the requested key
  159.     lresult = RegOpenKeyEx(hkey, SubKeyPath, ByVal 0&, KEY_ALL_ACCESS, phkResult)
  160.     ' Returns ERROR_SUCCESS if the key exist
  161.     If lresult = ERROR_SUCCESS Then
  162.         regKeyExist = True
  163.         lresult = RegCloseKey(phkResult)
  164.     Else
  165.         regKeyExist = False
  166.     End If
  167.  
  168. End Function
  169.  
  170. ' --------------------------------------------------------
  171. ' Purpose   : Creates new Value name in the registry
  172. '             hkey = Registry Root key
  173. '             SubKeyPath = String containing the keypath to create
  174. '             regDataType = either REG_DWORD or REG_SZ
  175. '             KeyName = String containing the key Name to create
  176. '             KeyValue = contains value to store in the registry
  177. ' Return    : True = Function Successful
  178. '             False = Function Failed
  179. ' Example   : result = CreateRegEntry(HKEY_LOCAL_MACHINE,_
  180. '                      "Software\ECLIPSE Development Software\PhoneBook",_
  181. '                       REG_SZ , "Phone Number", "(632) 888-3710")
  182. ' --------------------------------------------------------
  183. Public Function CreateRegEntry(hkey As RegRootKeys, SubKeyPath As String, Optional KeyDatatype As RegDataTypes, Optional KeyName As String, Optional KeyValue As Variant) As Boolean
  184. Dim lresult As Long
  185. Dim phkResult As Long
  186. Dim IsNewKey As Long
  187. Dim KeyValueLng As Long
  188. Dim KeyValueStr As String
  189.  
  190.     On Local Error GoTo CreateRegEntry_Err
  191.     ' Set path to your application's settings.
  192.     lresult = RegCreateKeyEx(hkey, SubKeyPath, 0&, REG_SZ, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, phkResult, IsNewKey)
  193.    
  194.     If Not (lresult = ERROR_SUCCESS) Then
  195.         CreateRegEntry = False
  196.         GoTo CreateRegEntry_End
  197.     End If
  198.  
  199.     ' Determine data type and use appropriate
  200.     ' passed value.
  201.     If Not IsMissing(KeyDatatype) Then
  202.         Select Case KeyDatatype
  203.             Case REG_DWORD
  204.                 KeyValueLng = KeyValue
  205.                 lresult = RegSetValueEx(phkResult, KeyName, ByVal 0&, KeyDatatype, KeyValueLng, Len(KeyValueLng))
  206.             Case REG_SZ
  207.                 KeyValueStr = KeyValue
  208.                 lresult = RegSetValueEx(phkResult, KeyName, ByVal 0&, KeyDatatype, ByVal KeyValueStr, Len(KeyValueStr))
  209.         End Select
  210.     
  211.         If Not (lresult = ERROR_SUCCESS) Then
  212.             CreateRegEntry = False
  213.             GoTo CreateRegEntry_End
  214.         End If
  215.     End If
  216.     CreateRegEntry = True
  217.  
  218. CreateRegEntry_End:
  219.     Exit Function
  220.  
  221. CreateRegEntry_Err:
  222.     CreateRegEntry = False
  223.     Resume CreateRegEntry_End
  224. End Function
  225.  
  226. ' --------------------------------------------------------
  227. ' Purpose   : Deletes an entire keypath from the registry
  228. '             (Use with extreme caution!)
  229. '             hkey = Registry Root key
  230. '             SubKeyPath = String containing the keypath to delete
  231. ' Return    : True = Function Successful
  232. '             False = Function Failed
  233. ' Example   : result = DeleteregEntry(HKEY_LOCAL_MACHINE,_
  234. '                      "Software\ECLIPSE Development Software\PhoneBook")
  235. ' --------------------------------------------------------
  236. Public Function DeleteRegEntry(hkey As RegRootKeys, SubKeyPath As String) As Boolean
  237. Dim lresult As Long
  238. Dim phkResult As Long
  239.  
  240.     On Local Error GoTo DeleteRegEntry_Err
  241.     
  242.     ' Open the application's path key.
  243.     lresult = RegOpenKeyEx(hkey, SubKeyPath, ByVal 0&, KEY_ALL_ACCESS, phkResult)
  244.     If Not (lresult = ERROR_SUCCESS) Then
  245.         DeleteRegEntry = False
  246.         GoTo DeleteRegEntry_End
  247.     End If
  248.  
  249.     ' Delete the entire application's path key and any
  250.     ' associated keys and values.
  251.     lresult = RegDeleteKey(hkey, SubKeyPath)
  252.    
  253.     If Not (lresult = ERROR_SUCCESS) Then
  254.         DeleteRegEntry = False
  255.         GoTo DeleteRegEntry_End
  256.     End If
  257.    
  258.     lresult = RegCloseKey(hkey)
  259.     DeleteRegEntry = True
  260.  
  261. DeleteRegEntry_End:
  262.     Exit Function
  263.    
  264. DeleteRegEntry_Err:
  265.     DeleteRegEntry = False
  266.     Resume DeleteRegEntry_End
  267. End Function
  268.  
  269. ' --------------------------------------------------------
  270. ' Purpose   : Deletes a key value rom the registry
  271. '             hkey = Registry Root key
  272. '             SubKeyPath = String containing the keypath to delete
  273. '             KeyName = String containing the key Name to delete
  274. ' Return    : True = Function Successful
  275. '             False = Function Failed
  276. ' Example   : result = DeleteRegValue(HKEY_LOCAL_MACHINE,_
  277. '                      "Software\ECLIPSE Development Software\PhoneBook",_
  278. '                       "Phone Number")
  279. ' --------------------------------------------------------
  280. Public Function DeleteRegValue(hkey As RegRootKeys, SubKeyPath As String, KeyName As String) As Boolean
  281. Dim lresult As Long
  282. Dim phkResult As Long
  283.  
  284.     On Local Error GoTo DeleteRegValue_Err
  285.     
  286.     ' Open the application's path key.
  287.     lresult = RegOpenKeyEx(hkey, SubKeyPath, ByVal 0&, KEY_ALL_ACCESS, phkResult)
  288.     
  289.     If Not (lresult = ERROR_SUCCESS) Then
  290.         DeleteRegValue = False
  291.         GoTo DeleteRegValue_End
  292.     End If
  293.     
  294.     lresult = RegDeleteValue(phkResult, KeyName)
  295.     If lresult = ERROR_SUCCESS Then
  296.         DeleteRegValue = True
  297.     Else
  298.         DeleteRegValue = False
  299.     End If
  300.     lresult = RegCloseKey(phkResult)
  301.  
  302. DeleteRegValue_End:
  303.     Exit Function
  304.     
  305. DeleteRegValue_Err:
  306.     DeleteRegValue = False
  307.     Resume DeleteRegValue_End
  308. End Function
  309.  
  310. ' --------------------------------------------------------
  311. ' Purpose   : Query an exisitng Value name in the registry
  312. '             hkey = Registry Root key
  313. '             SubKeyPath = String containing the keypath to query
  314. '             KeyName = String containing the key Name to query
  315. '             regDataType = either REG_DWORD or REG_SZ
  316. '             KeyValue = contains the result value that was queried
  317. '                        This will hold the same value as the RegValue
  318. '             DefaultValue = (optional) the value to return when the requested
  319. '                            key doesn't exist.
  320. ' Return    : True = Function Successful
  321. '             False = Function Failed
  322. ' Example   : result = GetRegValue(HKEY_LOCAL_MACHINE,_
  323. '                      "Software\ECLIPSE Development Software\PhoneBook",_
  324. '                       "Phone Number", REG_SZ , strPhNum, "(632) 888-3710")
  325. ' --------------------------------------------------------
  326.  
  327. Public Function GetRegValue(hkey As RegRootKeys, SubKeyPath As String, KeyName As String, KeyDatatype As RegDataTypes, KeyValue As Variant, Optional DefaultValue) As Boolean
  328. Dim lresult As Long
  329. Dim phkResult As Long
  330. Dim dwType As Long
  331. Dim cbData As Long
  332. Dim varStrData As String
  333. Dim varLngData As Long
  334.  
  335.     On Local Error GoTo GetRegValue_Err
  336.     regValue = ""   'clear previous value first
  337.     
  338.     ' Open the key for application's path.
  339.     lresult = RegOpenKeyEx(hkey, SubKeyPath, ByVal 0&, KEY_ALL_ACCESS, phkResult)
  340.     If Not (lresult = ERROR_SUCCESS) Then
  341.         GetRegValue = False
  342.         GoTo GetRegValue_End
  343.     End If
  344.    
  345.    ' Set up passed variables and retrieve value.
  346.     Select Case KeyDatatype
  347.         Case REG_SZ
  348.             varStrData = String$(255, 0)
  349.             cbData = LenB(varStrData)
  350.             lresult = RegQueryValueEx(phkResult, KeyName, ByVal 0&, dwType, ByVal varStrData, cbData)
  351.         Case REG_DWORD
  352.             varLngData = False
  353.             cbData = LenB(varLngData)
  354.             lresult = RegQueryValueEx(phkResult, KeyName, ByVal 0&, dwType, varLngData, cbData)
  355.     End Select
  356.  
  357.     If Not (lresult = ERROR_SUCCESS) Then
  358.         GetRegValue = False
  359.         GoTo GetRegValue_End
  360.     End If
  361.    
  362.     ' Select data type (for the needed types
  363.     ' used in the values) and assign value.
  364.     Select Case dwType
  365.         Case REG_NONE
  366.             KeyValue = ""
  367.         Case REG_SZ
  368.             KeyValue = Left$(varStrData, cbData)
  369.         Case REG_DWORD
  370.             KeyValue = varLngData
  371.         Case Else
  372.             KeyValue = ""
  373.     End Select
  374.     
  375.     GetRegValue = True
  376.     regValue = KeyValue
  377.     
  378.     ' Close key.
  379.     lresult = RegCloseKey(phkResult)
  380.  
  381. GetRegValue_End:
  382.     If Not IsMissing(DefaultValue) And GetRegValue = False Then
  383.         regValue = DefaultValue
  384.     End If
  385.     Exit Function
  386.  
  387. GetRegValue_Err:
  388.    Resume GetRegValue_End
  389. End Function
  390.  
  391. ' --------------------------------------------------------
  392. ' Purpose   : Stores a key Value into an existing name in the registry
  393. '             If the key doesn't exist, it will be created first.
  394. '             hkey = Registry Root key
  395. '             SubKeyPath = String containing the keypath
  396. '             KeyName = String containing the key Name to store the value
  397. '             regDataType = either REG_DWORD or REG_SZ
  398. '             NewKeyValue = contains new value to store in the registry
  399. ' Return    : True = Function Successful
  400. '             False = Function Failed
  401. ' Example   : result = SetRegValue(HKEY_LOCAL_MACHINE,_
  402. '                      "Software\ECLIPSE Development Software\PhoneBook",_
  403. '                       "Phone Number", REG_SZ , "(632) 888-3710")
  404. ' --------------------------------------------------------
  405.  
  406. Public Function SetRegValue(hkey As RegRootKeys, SubKeyPath As String, KeyName As String, KeyDatatype As RegDataTypes, NewKeyValue As Variant) As Boolean
  407. Dim lresult As Long
  408. Dim phkResult As Long
  409. Dim dwType As Long
  410. Dim cbData As Long
  411. Dim varStrData As String
  412. Dim varLngData As Long
  413. 'Dim Msg As String
  414.     On Local Error GoTo SetRegValue_Err
  415.  
  416.     ' Open the key for application's path.
  417.     lresult = RegOpenKeyEx(hkey, SubKeyPath, ByVal 0&, KEY_ALL_ACCESS, phkResult)
  418.     If Not (lresult = ERROR_SUCCESS) Then
  419.         ' the key may not yet exist, try to create new key
  420.         CreateRegEntry hkey, SubKeyPath, CLng(KeyDatatype), KeyName, NewKeyValue
  421.         ' Try to reopen the key the second time
  422.         lresult = RegOpenKeyEx(hkey, SubKeyPath, ByVal 0&, KEY_ALL_ACCESS, phkResult)
  423.         If Not (lresult = ERROR_SUCCESS) Then
  424.             SetRegValue = False
  425.             GoTo SetRegValue_End
  426.         End If
  427.     End If
  428.  
  429.     ' Set up passed variables and retrieve value.
  430.     Select Case KeyDatatype
  431.         Case REG_SZ
  432.             varStrData = NewKeyValue
  433.             lresult = RegSetValueEx(phkResult, KeyName, ByVal 0&, KeyDatatype, ByVal varStrData, Len(varStrData))
  434.         Case REG_DWORD
  435.             varLngData = CLng(NewKeyValue)
  436.             lresult = RegSetValueEx(phkResult, KeyName, ByVal 0&, KeyDatatype, varLngData, Len(varLngData))
  437.     End Select
  438.     
  439.     If Not (lresult = ERROR_SUCCESS) Then
  440.         SetRegValue = False
  441.         GoTo SetRegValue_End
  442.     End If
  443.  
  444.     ' Close key.
  445.     lresult = RegCloseKey(phkResult)
  446.     SetRegValue = True
  447.  
  448. SetRegValue_End:
  449.     Exit Function
  450.  
  451. SetRegValue_Err:
  452.     Resume SetRegValue_End
  453. End Function
  454.  
  455.